﻿//===============================================================================
// Microsoft Platform Architecture Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ServiceModel.Syndication;
using GeoRss;
using System.Xml;
using System.IO;

namespace GeoRSSTest
{
    /// <summary>
    /// Summary description for TestGeoRSSFeedBuilder
    /// </summary>
    [TestClass]
    public class GeoRssFeedBuilderFixture
    {
        public GeoRssFeedBuilderFixture()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [ExpectedException(typeof(ArgumentException))]
        [TestMethod]
        public void RSSBuilderWithEmptyDataThrows()
        {
            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(null);
        }

        [TestMethod]
        public void BuildsRssFeedWithSimpleData()
        {
            GeoRssData data = new GeoRssData("title", "description");
            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(data.Description, f.Description.Text);
            Assert.AreEqual(data.Title, f.Title.Text);
            Assert.AreEqual(0, f.Items.Count<SyndicationItem>());
        }

        [TestMethod]
        public void BuildsRssFeedWithOneItem()
        {
            GeoRssData data = new GeoRssData("title", "description");
            data.Items.Add(new GeoRssItem() { Title = "i_title", Description = "i_desc" });
            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(data.Description, f.Description.Text);
            Assert.AreEqual(data.Title, f.Title.Text);
            Assert.AreEqual(1, f.Items.Count<SyndicationItem>());
            Assert.AreEqual("i_title", f.Items.ElementAt<SyndicationItem>(0).Title.Text);
            Assert.AreEqual("i_desc", (f.Items.ElementAt<SyndicationItem>(0).Content as TextSyndicationContent).Text);
        }

        [TestMethod]
        public void BuildsRssFeedWithOnePoint()
        {
            GeoRssData data = new GeoRssData("title", "description");
            data.AddPoint("circle_title", "circle_content", 20, 10);
                        
            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(1, f.Items.Count<SyndicationItem>() );
            Assert.AreEqual(1, f.Items.ElementAt<SyndicationItem>(0).ElementExtensions.Count );

            StringBuilder sb = new StringBuilder();
            StringWriter s = new StringWriter(sb);
            XmlTextWriter tr = new XmlTextWriter( s );
            f.Items.ElementAt<SyndicationItem>(0).ElementExtensions[0].WriteTo(tr);
            tr.Close();
            s.Close();

            Assert.AreEqual("<georss:point xmlns:georss=\"http://www.georss.org/georss\">20 10</georss:point>", sb.ToString() );
        }

        [TestMethod]
        public void BuildsRssFeedWithDataOnePointAndIcon()
        {
            GeoRssData data = new GeoRssData("title", "description");
            data.AddPoint("point_title", "point_content", 20, 10).Icon = "My Icon";

            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(1, f.Items.Count<SyndicationItem>());
            Assert.AreEqual(2, f.Items.ElementAt<SyndicationItem>(0).ElementExtensions.Count);

            StringBuilder sb = new StringBuilder();
            StringWriter s = new StringWriter(sb);
            XmlTextWriter tr = new XmlTextWriter(s);

            (from e in f.Items.ElementAt<SyndicationItem>(0).ElementExtensions
             where e.OuterName == "point"
             select e).Single<SyndicationElementExtension>().WriteTo(tr); tr.Close();
            
            s.Close();
            
            Assert.AreEqual("<georss:point xmlns:georss=\"http://www.georss.org/georss\">20 10</georss:point>", sb.ToString());

            sb = new StringBuilder();
            s = new StringWriter(sb);
            tr = new XmlTextWriter(s);
            
            (from e in f.Items.ElementAt<SyndicationItem>(0).ElementExtensions
             where e.OuterName == "icon"
             select e).Single<SyndicationElementExtension>().WriteTo(tr); 

            tr.Close();
            s.Close();
            
            Assert.AreEqual("<icon>My Icon</icon>", sb.ToString());
        }

        [TestMethod]
        public void BuildsRssWithOnePointIconAndLink()
        {
            GeoRssData data = new GeoRssData("title", "description");
            GeoRssPointItem item = data.AddPoint("point_title", "point_content", 20, 10);
            item.Link = "somelink";
            item.Icon = "My Icon";


            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(1, f.Items.Count<SyndicationItem>());
            Assert.AreEqual(3, f.Items.ElementAt<SyndicationItem>(0).ElementExtensions.Count);

            StringBuilder sb = new StringBuilder();
            StringWriter s = new StringWriter(sb);
            XmlTextWriter tr = new XmlTextWriter(s);
            
            (from e in f.Items.ElementAt<SyndicationItem>(0).ElementExtensions
             where e.OuterName == "point"
             select e).Single<SyndicationElementExtension>().WriteTo(tr); 
            
            tr.Close();
            s.Close();

            Assert.AreEqual("<georss:point xmlns:georss=\"http://www.georss.org/georss\">20 10</georss:point>", sb.ToString());

            sb = new StringBuilder();
            s = new StringWriter(sb);
            tr = new XmlTextWriter(s);

            (from e in f.Items.ElementAt<SyndicationItem>(0).ElementExtensions
             where e.OuterName == "icon"
             select e).Single<SyndicationElementExtension>().WriteTo(tr); 
            
            tr.Close();
            s.Close();

            Assert.AreEqual("<icon>My Icon</icon>", sb.ToString());

            sb = new StringBuilder();
            s = new StringWriter(sb);
            tr = new XmlTextWriter(s);
            
            (from e in f.Items.ElementAt<SyndicationItem>(0).ElementExtensions
                      where e.OuterName == "link"
                  select e).Single<SyndicationElementExtension>().WriteTo(tr);
            
            tr.Close();
            s.Close();

            Assert.AreEqual("<link>somelink</link>", sb.ToString());
        }


        [TestMethod]
        public void BuildsRssWithOnePolygon()
        {
            GeoRssData data = new GeoRssData("title", "description");
            Point[] polygon = { new Point(10,11), new Point(20,21), new Point(10, 11)};
            data.AddPolygon("polygon_title", "polygon_content", polygon);

            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(1,f.Items.Count<SyndicationItem>());
            Assert.AreEqual(1,f.Items.ElementAt<SyndicationItem>(0).ElementExtensions.Count);

            StringBuilder sb = new StringBuilder();
            StringWriter s = new StringWriter(sb);
            XmlTextWriter tr = new XmlTextWriter(s);
            f.Items.ElementAt<SyndicationItem>(0).ElementExtensions[0].WriteTo(tr);
            tr.Close();
            s.Close();
            
            Assert.AreEqual( "<georss:polygon xmlns:georss=\"http://www.georss.org/georss\">10 11 20 21 10 11 </georss:polygon>", sb.ToString());
        }

        [TestMethod]
        public void BuildsRssWithOneLine()
        {
            GeoRssData data = new GeoRssData("title", "description");
            Point[] line = { new Point(10, 11), new Point(20, 21)};
            GeoRssLineItem item = data.AddLine("line_title", "line_content", line);
            
            SyndicationFeed f = GeoRssFeedBuilder.GetFeed(data);

            Assert.AreEqual(1, f.Items.Count<SyndicationItem>());
            Assert.AreEqual(1, f.Items.ElementAt<SyndicationItem>(0).ElementExtensions.Count);

            StringBuilder sb = new StringBuilder();
            StringWriter s = new StringWriter(sb);
            XmlTextWriter tr = new XmlTextWriter(s);
            f.Items.ElementAt<SyndicationItem>(0).ElementExtensions[0].WriteTo(tr);
            tr.Close();
            s.Close();

            Assert.AreEqual("<georss:line xmlns:georss=\"http://www.georss.org/georss\">10 11 20 21 </georss:line>",sb.ToString() );
        }
    }
}
